﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Common
{
    public class WebBase
    {
        private static string pwdKey = ConfigHelper.GetPwdKey();
        #region***重载Request.Form************************
        /// <summary>
        /// 重载Request.Form方法
        /// 带有默认值
        /// 返回值类型：int
        /// </summary>
        /// <param name="valueName">参数名称</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        public static int GetFormInt(string valueName, int defaultValue)
        {
            int rtnValue = 0;
            if (System.Web.HttpContext.Current.Request.Form[valueName] != null)
            {
                try
                {
                    rtnValue = Convert.ToInt32(System.Web.HttpContext.Current.Request.Form[valueName]);
                }
                catch
                {
                    rtnValue = 0;
                }
            }
            else
            {
                rtnValue = defaultValue;
            }
            return rtnValue;
        }

        /// <summary>
        /// 重载Request.Form
        /// 返回值类型：int
        /// </summary>
        /// <param name="valueName">参数名称</param>
        /// <returns></returns>
        public static int GetFormInt(string valueName)
        {
            int rtnValue = 0;
            if (System.Web.HttpContext.Current.Request.Form[valueName] != null)
            {
                try
                {
                    rtnValue = Convert.ToInt32(System.Web.HttpContext.Current.Request.Form[valueName]);
                }
                catch
                {
                    rtnValue = 0;
                }
            }
            return rtnValue;
        }
        /// <summary>
        /// 获取decimal参数
        /// </summary>
        /// <param name="valueName"></param>
        /// <returns></returns>
        public static decimal GetFormDecimal(string valueName)
        {
            decimal rtnValue = 0;
            if (System.Web.HttpContext.Current.Request.Form[valueName] != null)
            {
                try
                {
                    rtnValue = Convert.ToDecimal(System.Web.HttpContext.Current.Request.Form[valueName]);
                }
                catch
                {
                    rtnValue = 0;
                }
            }
            return rtnValue;
        }
        public static DateTime GetFormDateTime(string valueName)
        {
            DateTime rtnValue = DateTime.Now;
            if (System.Web.HttpContext.Current.Request.Form[valueName] != null)
            {
                try
                {
                    rtnValue = Convert.ToDateTime(System.Web.HttpContext.Current.Request.Form[valueName]);
                }
                catch
                {
                    rtnValue = DateTime.Now;
                }
            }
            return rtnValue;
        }

        /// <summary>
        /// 重载Request.Form
        /// 带有默认值
        /// 返回值类型：string
        /// </summary>
        /// <param name="valueName"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string GetForm(string valueName, string defaultValue)
        {
            return System.Web.HttpContext.Current.Request.Form[valueName] != null ? System.Web.HttpContext.Current.Request.Form[valueName] : defaultValue;
        }

        /// <summary>
        /// 重载Request.Form
        /// 返回值类型：string
        /// </summary>
        /// <param name="valueName">参数名称</param>
        /// <returns></returns>
        /// 
        public static string GetForm(string valueName)
        {
            return System.Web.HttpContext.Current.Request.Form[valueName] != null ? System.Web.HttpContext.Current.Request.Form[valueName] : "";
        }
        #endregion*********************************

        #region***重载Request.QueryString************************
        /// <summary>
        /// 重载Request.QueryString方法
        /// 带有默认值
        /// 返回值类型：int
        /// </summary>
        /// <param name="valueName">参数名称</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        public static int GetQueryInt(string valueName, int defaultValue)
        {
            int rtnValue = 0;
            if (System.Web.HttpContext.Current.Request.QueryString[valueName] != null)
            {
                try
                {
                    rtnValue = Convert.ToInt32(System.Web.HttpContext.Current.Request.QueryString[valueName]);
                }
                catch
                {
                    rtnValue = 0;
                }
            }
            else
            {
                rtnValue = defaultValue;
            }
            return rtnValue;
        }

        /// <summary>
        /// 重载Request.QueryString
        /// 返回值类型：int
        /// </summary>
        /// <param name="valueName">参数名称</param>
        /// <returns></returns>
        public static int GetQueryInt(string valueName)
        {
            int rtnValue = 0;
            if (System.Web.HttpContext.Current.Request.QueryString[valueName] != null)
            {
                try
                {
                    rtnValue = Convert.ToInt32(System.Web.HttpContext.Current.Request.QueryString[valueName]);
                }
                catch
                {
                    rtnValue = 0;
                }
            }
            return rtnValue;
        }

        /// <summary>
        /// 重载Request.QueryString
        /// 带有默认值
        /// 返回值类型：string
        /// </summary>
        /// <param name="valueName"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string GetQuery(string valueName, string defaultValue)
        {
            return System.Web.HttpContext.Current.Request.QueryString[valueName] != null ? System.Web.HttpContext.Current.Request.QueryString[valueName] : defaultValue;
        }

        /// <summary>
        /// 重载Request.QueryString
        /// 返回值类型：string
        /// </summary>
        /// <param name="valueName">参数名称</param>
        /// <returns></returns>
        public static string GetQuery(string valueName)
        {
            return System.Web.HttpContext.Current.Request.QueryString[valueName] != null ? System.Web.HttpContext.Current.Request.QueryString[valueName] : "";
        }
        #endregion*********************************

        /// <summary>
        /// 读取Set.Config AppSetting配置节中值
        /// </summary>
        /// <param name="paramName"></param>
        /// <returns></returns>
        public static string GetSetConfigValueStr(string paramName)
        {
            return System.Configuration.ConfigurationManager.AppSettings[paramName].ToString();

        }
        public static string GetFileName()
        {
            return DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString().PadLeft(2, '0') +
                   DateTime.Now.Day.ToString().PadLeft(2, '0') + DateTime.Now.Hour.ToString().PadLeft(2, '0') +
                   DateTime.Now.Minute.ToString().PadLeft(2, '0') + DateTime.Now.Second.ToString().PadLeft(2, '0') + new Random().Next(1, 9999).ToString();
        }

        public static string RepleaseContentStr(string str)
        {
            //替换包含Html中<>:<——&lt;    >——&gt;   "——&quot; 逐个替换
            string getres = str;
            getres = getres.Replace("<", "&lt;");
            getres = getres.Replace(">", "&gt;");
            getres = getres.Replace("\"", "&quot;");
            return getres;
        }
        /// <summary>
        ///加密
        /// </summary>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static string Encode(string pwd)
        {
            return HttpSecurity.Encode(pwd, pwdKey);
        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="pwd"></param>
        /// <returns></returns>

        public static string Decode(string pwd)
        {
            return HttpSecurity.Decode(pwd, pwdKey);
        }

    }
}
